package by.yativem.commons.messages;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;

public class Messages {
	
	public enum MessaegType{
		ERROR,
		WARNING,
		INFO
	}
	
	private static final Logger log = Logger.getLogger(Messages.class);
	
	private List<SimpleMessage> messages;
	
	public Messages() {
		messages = new ArrayList<SimpleMessage>();
	}
	
	public synchronized void addMessage(SimpleMessage message){
		messages.add(message);
	}
	
	public void addInfoMessage(String msg){
		addMessage(new SimpleMessage(msg,MessaegType.INFO));
	}
	
	public void addInfoMessage(String msg, Object... params ){
		addMessage(new SimpleMessage(msg,MessaegType.INFO , params));
	}
	
	
	public void addWarnMessage(String msg){
		addMessage(new SimpleMessage(msg,MessaegType.WARNING));
	}
	
	public void addWarnMessage(String msg, Object... params ){
		addMessage(new SimpleMessage(msg,MessaegType.WARNING , params));
	}
	
	
	public void addErrMessage(String msg){
		addMessage(new SimpleMessage(msg,MessaegType.ERROR));
	}
	
	public void addErrMessage(String msg, Object... params ){
		addMessage(new SimpleMessage(msg,MessaegType.ERROR , params));
	}
	
	/*
	 * return unmodifiable list of messages;
	 */
	
	public List<SimpleMessage> getMessages(){
		return Collections.unmodifiableList(messages);
	}
	
	/*
	 * return list of messages and clean current list of messages;
	 */
	
	public synchronized List<SimpleMessage> queue(){
		List<SimpleMessage> tmp = messages;
		messages = new ArrayList<SimpleMessage>();
		return tmp;
	}

}
